﻿using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;
using Xenta.Utils;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the file folder entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  + Extended
    ///   + Base
    ///    | - Parent(of the Primary level)
    ///    | - FileCount
    ///    | - Size
    ///    | - Flags
    ///    | - UpdatedOn
    ///    | - CreatedOn
    ///    | - Translations(of the same level)
    ///    + Keys
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class FileFolderEntity : AuditableEntity
    {
        #region Fields

        private FileFolderEntity _parent;
        private ICollection<FileFolderEntity> _childs;
        private ICollection<FileEntity> _files;
        private ICollection<TFileFolderEntity> _translations;

        #endregion

        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public FileFolderEntity()
        {
            _parent = null;
            _childs = new List<FileFolderEntity>();
            _files = new List<FileEntity>();
            _translations = new List<TFileFolderEntity>();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the parent folder identifier.
        /// </summary>
        public virtual int ParentID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the parent folder.
        /// </summary>
        public virtual FileFolderEntity Parent
        {
            get
            {
                return ParentID != 0 ? _parent : null;
            }
            set
            {
                _parent = value;
                ParentID = value != null ? value.EntityID : 0;
            }
        }

        /// <summary>
        /// Gets or sets the file count.
        /// </summary>
        public virtual int FileCount
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the size.
        /// </summary>
        public virtual long Size
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public virtual FileFolderFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Collections
        
        /// <summary>
        /// Gets or sets the collection of folders.
        /// </summary>
        public virtual ICollection<FileFolderEntity> Childs
        {
            get
            {
                return _childs;
            }
            set
            {
                _childs = value;
            }
        }

        /// <summary>
        /// Gets or sets the collection of files.
        /// </summary>
        public virtual ICollection<FileEntity> Files
        {
            get
            {
                return _files;
            }
            set
            {
                _files = value;
            }
        }

        /// <summary>
        /// Gets or sets the folder translations.
        /// </summary>
        public virtual ICollection<TFileFolderEntity> Translations
        {
            get
            {
                return _translations;
            }
            set
            {
                _translations = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the folder translation.
        /// </summary>
        /// <param name="languageID">The language identifier.</param>
        /// <returns>The folder translation.</returns>
        public virtual TFileFolderEntity Translation(int languageID)
        {
            return _translations.SingleOrDefault(x => x.Language.EntityID == languageID);
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Base)
            {
                obj["Parent"] = Parent != null
                    ? Parent.Disclose(DisclosureLevel.Primary, options)
                    : null;
                obj["FileCount"] = FileCount;
                obj["Size"] = Size;
                obj["Flags"] = Flags.ToString();
                obj["Translations"] = (options.ContainsKey("TranslatedTo")
                    ? Translations.Where(x => options["TranslatedTo"].Contains(x.Language.Code))
                    : Translations)
                    .Map(x => x.Disclose(level, options));
            }
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapping.
    /// </summary>
    public class FileFolderMapping : EntityMapping<FileFolderEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public FileFolderMapping()
        {
            Table("fwk_FileFolders");
            Id(x => x.EntityID, m =>
            {
                m.Column("FolderID");
                m.Generator(Generators.Identity);
            });
            Property(x => x.ParentID, m =>
            {
                m.Column("ParentFolderID");
                m.NotNullable(true);
            });
            ManyToOne(x => x.Parent, m =>
            {
                m.Column("ParentFolderID");
                m.NotFound(NotFoundMode.Ignore);
                m.Insert(false);
                m.Update(false);
            });
            Property(x => x.FileCount, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.Size, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
            Bag(x => x.Childs, m =>
            {
                m.Table("fwk_FileFolders");
                m.Key(k =>
                {
                    k.Column("ParentFolderID");
                    k.NotNullable(true);
                });
                m.Inverse(true);
                m.BatchSize(10);
            },
            a =>
            {
                a.OneToMany();
            });
            Bag(x => x.Files, m =>
            {
                m.Table("fwk_Files");
                m.Key(k =>
                {
                    k.Column("FolderID");
                    k.NotNullable(true);
                });
                m.Inverse(true);
            },
            a =>
            {
                a.OneToMany();
            });
            Bag(x => x.Translations, m =>
            {
                m.Table("fwk_TFileFolders");
                m.Key(k =>
                {
                    k.Column("FolderID");
                    k.ForeignKey("FK_fwk_TFileFolders_fwk_FileFolders");
                    k.NotNullable(true);
                });
                m.Inverse(true);
                m.BatchSize(10);
            },
            a =>
            {
                a.OneToMany();
            });
        }
    }
}
